version 14
clear
set more off
set matsize 5000
pwd

****************************************************************************
******5) USE KID TAX DATA AND TACK ON "FIRST STAGE" EITC INFO***************
****************************************************************************
use  pik  dob_*  using  ${raw_data}/irs1040_universe_kid.dta, clear
sort pik
by pik: gen n=_n
keep if n==1
drop n

gen year=2019
tempfile ty2019
save `ty2019'

use ${raw_data}/irs1040_universe_kid.dta, clear
append using ${raw_data}/irs1040_ty17_18_kid.dta
append using `ty2019'

keep if dob_month==1 | dob_month==2 |dob_month==11 |dob_month==12

destring pik, replace

sort pik year
xtset pik year

foreach x in dob_month dob_day dob_year dod_year {
	replace `x' = l.`x' if `x'==. & l.`x'~=.
}

rename year taxyr_out

********** Restrict to Kid's Tax Years at age 19+ **********
gen dob = mdy(dob_month, dob_day, dob_year)
format dob %tdnn/dd/CCYY

gen cutyr=.
forvalues yr=1970/2017 {
	replace cutyr = `yr' if inrange(dob,mdy(7,1,`yr'-1),mdy(6,30,`yr'))
}

gen agecyr = taxyr_out-cutyr
keep if agecyr>=19

drop dod_day dod_month dod_year dob cutyr

********** Merge on parent info from tax year relevant to birthdate cutoff **********
merge m:1 pik using ${clean_data}/taxyrcut_parents_small, keep(match master) gen(_m_parents) ///
	keepusing(taxyr_cut cutyr belowcut z /*pik_key_parent  dad_age*/ mom_age *parent_fil_stat parent_maxage birth_order* i_obstaxyr *parent_have_1040 i_*eitc* *deitc* *agi* *dtax* *move* *parent_mafid* dob_dow pob_fips male us_born)

sum if _m_parents==1
list in 1/10000 if _m_parents==1
keep if _m_parents==3

********** Clean up variable names **********
do ${main}/dos/tax/build/build_support/kid_parentvar_cleanup_slim.do

gen i_k1 = (birth_order==1)
save ${clean_data}/taxtax_clean, replace

**************************************************************************
***6) Set up kid outcomes
**************************************************************************
do ${main}/dos/tax/build/build_support/kid_outcomes_slim.do

***********CLEAN UP*************
describe

capture drop _m_*
capture drop cpi_jan cpi_2015 f f2
capture drop *unadj*

**************************************************************************
***7) KID RACE
**************************************************************************
merge m:1 pik using ${raw_data}/dec2010_clean, keep(match master) gen(_m_dec2010)
drop _m_dec2010

save ${clean_data}/taxtax_clean, replace
